﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CILADA.Engine
{
    public abstract class AnalyserBase
    {
        #region Fields
        protected ICollection<string> _assemblyPaths;
        protected AssemblyReader _assemReader = new AssemblyReader();
        protected string _analysisType = "";
        #endregion

        #region Properties
        public int AssemblyNotFoundCount { get; protected set; }
        public int NotDotNetAssemblyCount { get; protected set; }
        public int ErrorDuringProcessingCount { get; protected set; }
        public string OutputDirectory { get; set; }
        public string OutputFileName { get; set; }
        public bool VerboseOutput { get; set; }
        #endregion

        #region Constructor
        public AnalyserBase()
        {
            ClearStats();
            this.VerboseOutput = true;
        }
        #endregion

        #region Delegates and Events
        public delegate void AssemblyAnalysedEvent(object from, AssemblyAnalysedEventArgs args);
        public event AssemblyAnalysedEvent AssemblyAnalysed;
        #endregion

        #region Instance Methods

        public abstract string AnalyseAssemblies(ICollection<string> assemblyPaths);

        public abstract void AnalyseAssembly(string assemblyPath);

        protected virtual void OnAssemblyAnalysed(AssemblyAnalysedEventArgs e)
        {
            if (null != AssemblyAnalysed)
            {
                AssemblyAnalysed(this, e);
            }
        }

        protected string GetAnalysisResults(int totalAssembliesGiven, string analysisType, TimeSpan analysisTimeSpan)
        {
            int successfullyAnalysed = totalAssembliesGiven - this.AssemblyNotFoundCount - this.ErrorDuringProcessingCount - this.NotDotNetAssemblyCount;

            StringBuilder sb = new StringBuilder();
            sb.Append(analysisType);
            sb.Append(" ");
            sb.AppendLine("Dependency Analysis of " + totalAssembliesGiven + " files is complete in " + analysisTimeSpan);

            sb.AppendLine(successfullyAnalysed + " successfully analysed assemblies.");
            sb.AppendLine("\tskipped " + this.AssemblyNotFoundCount + " could not be found.");
            sb.AppendLine("\t" + this.NotDotNetAssemblyCount + " were not .NET assemblies.");
            sb.AppendLine("\t" + this.ErrorDuringProcessingCount + " encountered errors during processing.");

            return sb.ToString();
        }

        public virtual void ClearStats()
        {
            this.AssemblyNotFoundCount = 0;
            this.NotDotNetAssemblyCount = 0;
            this.ErrorDuringProcessingCount = 0;
        }

        protected static string GetStringOrNull(Object obj)
        {
            return (null != obj) ? obj.ToString() : "null";
        }
        #endregion
    }
}
